#include "Computer.h"

Computer::Computer(const char *brand, float price)
: _brand(new char[strlen(brand) + 1]())
, _price(price)
{
    cout << "Computer(const char *, float)" << endl;
    /* _brand = new char[strlen(brand) + 1]();//申请空间，防止越界 */
    strcpy(_brand, brand);
}

#if 0
//编译器默认生成的拷贝构造函数不行
Computer::Computer(const Computer &rhs)
: _brand(rhs._brand)//浅拷贝
, _price(rhs._price)
{
    cout << "Computer(const Computer &)" << endl;
}
#endif

//自定义拷贝构造函数
//Q1:拷贝构造函数参数中的引用符号可以去掉吗？
//
//Q2:拷贝构造函数参数中的const关键字可以去掉吗？
//
Computer::Computer(const Computer &rhs)
: _brand(new char[strlen(rhs._brand) + 1]())//先申请空间,深拷贝
, _price(rhs._price)
{
    cout << "Computer(const Computer &)" << endl;//拷贝
    strcpy(_brand, rhs._brand);
}

void Computer::setBrand(const char *brand)
{
    strcpy(_brand, brand);
}

void Computer::setPrice(float price)
{
    _price = price;
}

void Computer::print()
{
    cout << "_brand = " << _brand << endl
         << "_price = " << _price << endl;
}

//析构函数的清理操作有时候需要手动清理
Computer::~Computer()
{
    cout << "~Computer()" << endl;
    if(_brand)//_brand != nullptr
    {
        /* cout << "delete " << endl; */
        delete [] _brand;
        _brand = nullptr;
    }
}
